﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace Royaume
{
    public partial class Admin : System.Web.UI.Page
    {
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["pkUser"] == null)
            {
                Response.Redirect("WebForm1.aspx");
            }
            if (!Page.IsPostBack) {
                this.BonjourAdmin.InnerText += Session["nomUser"].ToString();
                
                this.remplirdivinite();
              
            }
        }
        private void remplirdivinite() {
            Connect cn = new Connect();
            SqlDataReader dr ;
            cn.connectiongameIf();

            this.cmd = new SqlCommand("spGetdivinite", cn.myCn);
            this.cmd.CommandType = CommandType.StoredProcedure;
            dr = cmd.ExecuteReader();

            this.cboDivinite.DataTextField = "description";
            this.cboDivinite.DataValueField = "PkDivinite";

            this.cboDivinite.DataSource = dr;
            this.cboDivinite.DataBind();
            cn.connectionClose();
        }
 

      

        protected void btnValiderDivinite_Click(object sender, EventArgs e)
        {
             Connect cn = new Connect();
             cn.connectiongameIf();
            
            this.cmd = new SqlCommand("spDivinite", cn.myCn);
            this.cmd.CommandType = CommandType.StoredProcedure;
            this.cmd.Parameters.Add("@pkMembre", SqlDbType.Int).Value = Convert.ToInt32(this.gvListuser.SelectedDataKey["pk"]);
            this.cmd.Parameters.Add("@pkdivinite", SqlDbType.Int).Value = this.cboDivinite.SelectedValue;
            this.cmd.Parameters.Add("@Pourcentage", SqlDbType.Int).Value = Convert.ToInt32(this.txtPourcentageDivinite.Text);
            if (this.cmd.ExecuteNonQuery() != 0)
            {
                cUtility.fnMsgBox("Geste Divin effectué", Page, this);
            }
            else {
                cUtility.fnMsgBox("Désoler il y a eu erreur d'exécution", Page, this);
            }
            this.gvListuser.DataSourceID = "dsInfoAdmin";
            this.gvListuser.DataBind();
            cn.connectionClose();
        }
        

        protected void gvListuser_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnValiderDivinite.Enabled = true;

            this.lblUserName.Text="Nom d'usager: " + this.gvListuser.SelectedRow.Cells[2].Text;
           
          
            
            this.lblInfo.Text = "État (1=actif 0=banni) : " + this.gvListuser.SelectedDataKey["actif"].ToString() + "<br/>";
            this.lblInfo.Text += "Password : " + this.gvListuser.SelectedRow.Cells[3].Text + "<br/>";
            this.lblInfo.Text += "Race : " + this.gvListuser.SelectedRow.Cells[4].Text + "<br/>";
            this.lblInfo.Text += "Alignement : " + this.gvListuser.SelectedRow.Cells[5].Text + "<br/>";
            this.lblInfo.Text += "pop : " + this.gvListuser.SelectedRow.Cells[6].Text + "<br/>";
            this.lblInfo.Text += "minerais : " + this.gvListuser.SelectedRow.Cells[7].Text + "<br/>";
            this.lblInfo.Text += "bois : " + this.gvListuser.SelectedRow.Cells[8].Text + "<br/>";
            this.lblInfo.Text += "position : " + this.gvListuser.SelectedRow.Cells[9].Text + "<br/>";
            this.lblInfo.Text += "nom : " + this.gvListuser.SelectedRow.Cells[10].Text + "<br/>";
            this.lblInfo.Text += "prenom : " + this.gvListuser.SelectedRow.Cells[11].Text + "<br/>";
            this.lblInfo.Text += "Âge : " + this.gvListuser.SelectedRow.Cells[12].Text + "<br/>";
            this.lblInfo.Text += "Sexe : " + this.gvListuser.SelectedRow.Cells[13].Text + "<br/>";
            this.lblInfo.Text += "Ville : " + this.gvListuser.SelectedRow.Cells[14].Text + "<br/>";
            this.lblInfo.Text += "Email : " + this.gvListuser.SelectedDataKey["email"].ToString() + "<br/>";
            this.lblInfo.Text += "Nombre d'avertissement : " + this.gvListuser.SelectedDataKey["nbavertissement"].ToString() + "<br/>";
            this.lblInfo.Text += "Date dernier avertissement : " + this.gvListuser.SelectedDataKey["dateavertissement"].ToString() + "<br/>";
            this.lblInfo.Text += "Retour au jeu : " + this.gvListuser.SelectedDataKey["dateretour"].ToString() + "<br/>";
            this.Banir.Visible = false;
            this.Debanir.Visible = false;
            if (Convert.ToInt32(this.gvListuser.SelectedDataKey["actif"]) == 1)
            {
                this.Banir.Visible = true;
            }
            else {
                this.Debanir.Visible = true;
            }
        }

        protected void Banir_Click(object sender, EventArgs e)
        {
            Connect cn = new Connect();
            int usageraBanir = Convert.ToInt32(this.gvListuser.SelectedDataKey["pk"]);
            cn.connectionuserIf();
            
            this.cmd = new SqlCommand("spBanningPeaple", cn.myCn);
            this.cmd.CommandType = CommandType.StoredProcedure;
            this.cmd.Parameters.Add("@pkMembre", SqlDbType.Int).Value = Convert.ToInt32(this.gvListuser.SelectedDataKey["pk"]);
            if (this.cmd.ExecuteNonQuery() != 0)
            {
                this.lblInfo.Text = "LE JOUEUR A ÉTÉ BANNI!!!";
                this.Banir.Visible = false;
                this.Debanir.Visible = true;
            }
            else
            {
                this.lblInfo.Text = "IL Y A EU ERREUR D'EXÉCUTION, VEUILLEZ SÉLECTIONNER LE JOUEUR A NOUVEAU ET RÉESSAYER";
            }
            this.gvListuser.DataSourceID = "dsInfoAdmin";
            this.gvListuser.DataBind();

            cn.connectionClose();
            this.btnValiderDivinite.Enabled = false;
        }

        protected void Debanir_Click(object sender, EventArgs e)
        {
            Connect cn = new Connect();
            cn.connectionuserIf();
            
            int usageraBanir = Convert.ToInt32(this.gvListuser.SelectedDataKey["pk"]);
            cn.connectionuserIf();
            this.cmd = new SqlCommand("spDeBanPeaple", cn.myCn);
            this.cmd.CommandType = CommandType.StoredProcedure;
            this.cmd.Parameters.Add("@pkMembre", SqlDbType.Int).Value = Convert.ToInt32(this.gvListuser.SelectedDataKey["pk"]);
            if (this.cmd.ExecuteNonQuery() != 0)
            {
              
                this.lblInfo.Text = "LE JOUEUR A ÉTÉ DÉBANNI!!!";
                this.Banir.Visible = true;
                this.Debanir.Visible = false;
            }
            else
            {
                this.lblInfo.Text = "IL Y A EU ERREUR D'EXÉCUTION, VEUILLEZ SÉLECTIONNER LE JOUEUR A NOUVEAU ET RÉESSAYER";
            }
            this.gvListuser.DataSourceID = "dsInfoAdmin";
            this.gvListuser.DataBind();
            
            cn.connectionClose();
            this.btnValiderDivinite.Enabled = false;
        }

        protected void gvListuser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues["loginname"] != null)
            {

                int verifloginname = 0;
                if (e.OldValues["loginname"].ToString() != e.NewValues["loginname"].ToString())
                {
                    for (int i = 0; i < gvListuser.Rows.Count; i++)
                    {
                        if (gvListuser.Rows[i].Cells[2].Text.ToString() == e.NewValues["loginname"].ToString())
                        {
                            verifloginname++;
                        }
                    }
                    if (verifloginname > 0)
                    {
                        cUtility.fnMsgBox("Ce login est déja utilisé", Page, this);

                        // this.gvListuser.SelectedRow.Cells[2].BackColor = Color.FromName("Red");

                        e.Cancel = true;
                    }
                }


                if (e.OldValues["password"].ToString() != e.NewValues["password"].ToString())
                {
                    e.NewValues["password"] = cUtility.Encrypt(e.NewValues["password"].ToString());
                }
            }
            else {
                cUtility.fnMsgBox("Veuillez entrer un login ou annulé la commande!", Page, this);
                e.Cancel = true;
            }
            
        }

        protected void btnRecherche_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvListuser.Rows.Count; i++)
            {
                for (int y = 2; y < gvListuser.Columns.Count; y++)
                {
                    if (gvListuser.Rows[i].Cells[y].Text.ToString() == this.txtRechercheUser.Text.ToString()) 
                    {
                        this.gvListuser.SelectRow(i);
                        return;
                    }
                }
            }
            cUtility.fnMsgBox("Aucun élément corespondant!!!", Page, this);
        }

     

        
    }
}